package com.yanqu.road.server.gameplayer.module.activity.chess;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.chinesechess.*;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.activity.chinesechess.eChineseChessGameStatusType;
import com.yanqu.road.entity.enums.activity.chinesechess.eChineseChessLogTypeType;
import com.yanqu.road.entity.enums.activity.chinesechess.eChineseChessPiecesType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.bussiness.activity.ChineseChessBusiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.activity.ChineseChessProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.chinesechess.ChineseChessMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChineseChessPracticeModule extends GeneralModule {

    private ChineseChessPracticeUserData practiceData;

    public ChineseChessPracticeModule(GamePlayer player) {
        super(player);
    }

    public ChineseChessPracticeUserData getUserData(){
        return practiceData;
    }

    public void initPracticeUserData(int activityId){
        if(practiceData == null || practiceData.getActivityId() != activityId){
            ChineseChessPracticeUserData tempUserData = new ChineseChessPracticeUserData();
            tempUserData.setActivityId(ChineseChessMgr.getShowTimeActivityId());
            tempUserData.setUserId(getUserId());
            tempUserData.setScore(BigInteger.ZERO);
            tempUserData.setStep(0);
            tempUserData.setChessCount(1);
            tempUserData.setGameNo(0);
            tempUserData.setShuaiTimes(0);
            tempUserData.setIngotReviveTimes(0);
            tempUserData.setPowerTimes(0);
            tempUserData.setGameStatus(eChineseChessGameStatusType.Over.getValue());
            tempUserData.setUpdateTime(System.currentTimeMillis());
            Map<Integer, ChineseChessData> chessMap = new ConcurrentHashMap<>();
            tempUserData.setChessMap(chessMap);
            Map<Integer, ChineseChessData> nextChessMap = new ConcurrentHashMap<>();
            tempUserData.setNextChessMap(nextChessMap);
            tempUserData.setReviveSelf(false);
            tempUserData.setInsertOption();
            practiceData = tempUserData;
        }
    }
    
    public ChineseChessModule getChessModule(){
        return player.getModule(ChineseChessModule.class);
    }


    public void canRevive() {
        practiceData.setReviveSelf(true);
    }

    public int gameInto(int activityId){
        if(activityId != practiceData.getActivityId()){
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        if(practiceData.getGameStatus() == eChineseChessGameStatusType.Over.getValue()){
            newGameDataNewVersion(practiceData);
        }
        ChineseChessProto.ChineseChessIntoRespMsg.Builder msg = ChineseChessProto.ChineseChessIntoRespMsg.newBuilder();
        msg.setRet(0);
        msg.setGameNo(practiceData.getGameNo());
        List<ChineseChessData> chessList = new ArrayList<>(practiceData.getChessMap().values());
        for(ChineseChessData chess : chessList){
            if(chess.getChessNo() != 0){
                msg.addChess(getChessModule().parseChineseChessDataPb(chess));
            }
        }
        List<ChineseChessData> nextChessList = new ArrayList<>(practiceData.getNextChessMap().values());
        msg.setNextTurn(practiceData.getNextChessTurn());
        for(ChineseChessData chess : nextChessList){
            msg.addNextChess(getChessModule().parseChineseChessDataPb(chess));
        }
        msg.setSelfChess(getChessModule().parseChineseChessDataPb(practiceData.getChessMap().get(0)));
        msg.setGameStatus(practiceData.getGameStatus());
        msg.setReviveSelf(practiceData.getReviveSelfTimes());
        msg.setReviveTimes(practiceData.getIngotReviveTimes());
        msg.setScore(practiceData.getScore().longValue());
        msg.setIsReviveSelf(practiceData.isReviveSelf());
        msg.setPowerTimes(practiceData.getPowerTimes());
        msg.setStep(practiceData.getStep());
        msg.setPassTimes(practiceData.getPassTime());
        player.sendPacket(Protocol.U_CHINESECHESS_GAME_INFO_PRACTICE, msg);
        return 0;
    }
    

    public ChineseChessPracticeUserData newGameDataNewVersion(ChineseChessPracticeUserData practiceData){
        //只有2的状态才能重新开始游戏
        if(practiceData.getGameStatus() != eChineseChessGameStatusType.Over.getValue()){
            return null;
        }
        ChineseChessData self = new ChineseChessData();
        self.setChessNo(0);
        self.setChessId(eChineseChessPiecesType.Rooks.getValue());
        ChineseChessPosition chessPosition = ChineseChessMgr.getSelfPosition();
        self.setPosition(chessPosition);
        practiceData.setGameNo(practiceData.getGameNo() + 1);
        practiceData.setStep(0);
        practiceData.setScore(BigInteger.ZERO);
        practiceData.setShuaiTimes(0);
        practiceData.setReviveSelfTimes(0);
        practiceData.setPowerTimes(0);
        practiceData.setIngotReviveTimes(0);
        Map<Integer, ChineseChessData> chessMap = new ConcurrentHashMap<>();
        chessMap.put(self.getChessNo(),self);
        practiceData.setChessMap(chessMap);
        List<ChineseChessData> initialEnemyChessList = ChineseChessMgr.getInitialEnemyChessList();
        for(ChineseChessData chess : initialEnemyChessList){
            chessMap.put(chess.getChessNo(),chess);
        }
        practiceData.setChessCount(practiceData.getChessCount() + initialEnemyChessList.size());
        practiceData.setNextChessTurn(ChineseChessMgr.getNextChessTurn(practiceData.getStep()));
        Map<Integer,ChineseChessData> nextChessMap = ChineseChessMgr.getNextChessMap(practiceData.getStep(),practiceData.getChessCount());
        if(nextChessMap == null){
            log.error("练习模式生成棋子配置异常");
        }else{
            practiceData.setChessCount(practiceData.getChessCount() + nextChessMap.size());
        }
        practiceData.setGameStatus(eChineseChessGameStatusType.Running.getValue());
        practiceData.setChessMap(chessMap);
        practiceData.setNextChessMap(nextChessMap);
        return practiceData;
    }

    public int gameMoveVersion2(int x,int y,boolean isPowerOn){
        boolean isDieWithNoPower = isGoNoPowerDie();
        boolean isNoEatMove = false;
        if(practiceData.getStep() < ChineseChessMgr.getNewPlayerStep() && isDieWithNoPower){
            isNoEatMove = true;
        }
        boolean gameOver = false;
        if(practiceData.getStep() + 1 >= ChineseChessMgr.getGameOverStep()){
            gameOver = true;
        }
        int moveRet = checkSelfMove(x,y,isPowerOn);
        if(moveRet != 0){
            return moveRet;
        }
        int powerItemId = ChineseChessMgr.getPowerItemId();
        Property cost = PropertyHelper.parseStringToProperty(powerItemId + "=" + 1);
        if(isPowerOn){
            if(!player.getModule(CurrencyModule.class).currencyIsEnough(cost)){
                return GameErrorCode.E_CHINESECHESS_GAME_NO_POWER_ITEM;
            }
        }
        //原先的棋谱
        Map<Integer, ChineseChessData> oChessMap = new ConcurrentHashMap<>(practiceData.getChessMap());
        Map<Integer, ChineseChessData> chessMap = practiceData.getChessMap();
        ChineseChessData playerChess = chessMap.get(0);
        //原先的位置，新手保护要回滚回去
        ChineseChessPosition oPos = new ChineseChessPosition(playerChess.getPosition().getX(),playerChess.getPosition().getY());
        long eatChessAddScore = 0;
        List<ChineseChessData> eatChessList = new ArrayList<>();

        if(isPowerOn || practiceData.getPowerTimes() > 0){
            Map<String,ChineseChessPosition> posMap = new ConcurrentHashMap<>();
            if(isPowerOn){
                posMap.putAll(ChineseChessMgr.getPowerOnMovePosMap(playerChess,x,y));
            }
            if(practiceData.getPowerTimes() > 0){
                practiceData.setPowerTimes(practiceData.getPowerTimes() - 1);
                posMap.putAll(ChineseChessMgr.getKingPowerOnEatPosMap(x,y));
            }
            for(ChineseChessData chess : chessMap.values()){
                if(chess.getChessNo() == 0)continue;
                if(posMap.containsKey(chess.getPosition().getX() + "-" + chess.getPosition().getY())){
                    if(chess.getChessId() == eChineseChessPiecesType.King.getValue()){
                        practiceData.setPowerTimes(ChineseChessMgr.getEatKingPowerTime());
                    }
                    eatChessList.add(chess);
                    eatChessAddScore += getChessModule().getSkinAddScore(ChineseChessMgr.getChessConfig(chess.getChessId()));
                }
            }
        }else{
            for(ChineseChessData chess : chessMap.values()){
                if(chess.getChessNo() == 0)continue;
                if(chess.getPosition().getX() == x && chess.getPosition().getY() == y){//吃了其他棋子
                    if(chess.getChessId() == eChineseChessPiecesType.King.getValue()){
                        practiceData.setPowerTimes(ChineseChessMgr.getEatKingPowerTime());
                    }
                    eatChessList.add(chess);
                    eatChessAddScore = getChessModule().getSkinAddScore(ChineseChessMgr.getChessConfig(chess.getChessId()));
                }
            }
        }
        if(eatChessList.size() > 0){
            for(ChineseChessData eatChess : eatChessList){
                chessMap.remove(eatChess.getChessNo());
            }
        }
        playerChess.getPosition().setX(x);
        playerChess.getPosition().setY(y);
        //AI走棋
        ChineseChessData moveChess = ChineseChessMgr.getNextMove(chessMap,isNoEatMove);
        ChineseChessProto.ChineseChessMoveRespMsg.Builder msg = ChineseChessProto.ChineseChessMoveRespMsg.newBuilder();
        msg.setRet(0);
        //被吃了游戏结束
        if(moveChess != null && moveChess.getPosition().getX() == playerChess.getPosition().getX() && moveChess.getPosition().getY() == playerChess.getPosition().getY()){
            //新手保护直接不让走这一步，客户端自己表现被吃了又回滚
            if(practiceData.getStep() < ChineseChessMgr.getNewPlayerStep() && !gameOver){
                //编号0的是玩家自己
                oChessMap.get(0).setPosition(oPos);
                msg.setMoveChess(getChessModule().parseChineseChessDataPb(moveChess));
                moveChess.setPosition(moveChess.getLastPosition());
                practiceData.setChessMap(oChessMap);
                msg.setRet(GameErrorCode.E_CHINESECHESS_GAME_NOVICE);
                for(ChineseChessData chess : oChessMap.values()){
                    if(chess.getChessNo() == 0)continue;
                    msg.addChess(getChessModule().parseChineseChessDataPb(chess));
                }
                msg.setSelfChess(getChessModule().parseChineseChessDataPb(oChessMap.get(0)));
                msg.setScore(practiceData.getScore().longValue());
                player.sendPacket(Protocol.U_CHINESECHESS_GAME_MOVE_PRACTICE, msg);
                return 0;
            }
            //有替身复活效果，直接原地复活
            if(practiceData.isReviveSelf() && practiceData.getReviveSelfTimes() == 0 && !gameOver){
                practiceData.setReviveSelfTimes(1);
                //原地复活直接吃了
                chessMap.remove(moveChess.getChessNo());
                if(moveChess.getChessId() == eChineseChessPiecesType.King.getValue()){
                    practiceData.setPowerTimes(ChineseChessMgr.getEatKingPowerTime());
                }
                ChineseChessConfig chessConfig = ChineseChessMgr.getChessConfig(moveChess.getChessId());
                eatChessAddScore += getChessModule().getSkinAddScore(chessConfig);
                msg.setIsRevive(1);
            }else{
                practiceData.setGameStatus(eChineseChessGameStatusType.Revive.getValue());
            }
        }
        if(isPowerOn){
            
        }

        if(eatChessAddScore != 0){
            practiceData.setScore(practiceData.getScore().add(BigInteger.valueOf(eatChessAddScore)));
        }

        Map<Integer, ChineseChessData> nextChessMap = practiceData.getNextChessMap();
        practiceData.setStep(practiceData.getStep() + 1);
        //生成下一轮棋子
        if(practiceData.getNextChessTurn() - 1 == 0){
            //生成帅和普通的不一样
            if(isKingNext(nextChessMap)){
                Map<Integer,ChineseChessData> makeMap = ChineseChessMgr.makeNextChessPosKing(practiceData.getStep(),nextChessMap,chessMap);
                if(makeMap != null){
                    chessMap.putAll(makeMap);
                }
            }else{
                Map<Integer,ChineseChessData> makeMap = ChineseChessMgr.makeNextChessPos(practiceData.getStep(),nextChessMap,chessMap);
                if(makeMap != null){
                    chessMap.putAll(makeMap);
                }
            }
            makeNextNewVersion();
        }else{
            if(practiceData.getNextChessTurn() > 0){
                practiceData.setNextChessTurn(practiceData.getNextChessTurn() - 1);
            }
        }
        practiceData.setChessMap(chessMap);


        for(ChineseChessData chess : chessMap.values()){
            if(chess.getChessNo() == 0)continue;
            msg.addChess(getChessModule().parseChineseChessDataPb(chess));
        }
        msg.setSelfChess(getChessModule().parseChineseChessDataPb(chessMap.get(0)));
        if(moveChess != null){
            msg.setMoveChess(getChessModule().parseChineseChessDataPb(moveChess));
        }
        msg.setNextTurn(practiceData.getNextChessTurn());
        for(ChineseChessData chess : practiceData.getNextChessMap().values()){
            if(chess.getChessNo() == 0)continue;
            msg.addNextChess(getChessModule().parseChineseChessDataPb(chess));
        }
        msg.setGameStatus(practiceData.getGameStatus());
        msg.setScore(practiceData.getScore().longValue());
        msg.setStep(practiceData.getStep());

        //如果场上没有棋子吃了也没有生成了也是直接通关
        if(practiceData.getNextChessMap().size() == 0 && practiceData.getChessMap().size() == 1){
            gameOver = true;
        }
        
        if(gameOver){
            practiceData.setPassTime(practiceData.getPassTime() + 1);
            practiceData.setGameStatus(eChineseChessGameStatusType.Pass.getValue());
            msg.setPassTimes(practiceData.getPassTime());
            //如果场上没有棋子吃了也没有生成了也是直接通关 和指定通关步数的差值转换成分数给用户
            int passStep = ChineseChessMgr.getGameOverStep() - practiceData.getStep();
            if(passStep >= 0){
                practiceData.setScore(practiceData.getScore().add(BigInteger.valueOf(passStep * ChineseChessMgr.getPassStepAdd())));
            }
        }else{
            msg.setPassTimes(0);
        }
        player.sendPacket(Protocol.U_CHINESECHESS_GAME_MOVE_PRACTICE, msg);
        return 0;
    }


    public boolean isGoNoPowerDie(){
        if(practiceData.getGameStatus() != eChineseChessGameStatusType.Running.getValue()){
            return true;
        }
        Map<Integer, ChineseChessData> chessMap = practiceData.getChessMap();
        ChineseChessPosition pos = ChineseChessMgr.debugAutoMove(chessMap,false);
        if(pos != null){
            return false;
        }else{
            return true;
        }
    }
    
    private boolean isKingNext(Map<Integer, ChineseChessData> nextChessMap){
        for(ChineseChessData chess : nextChessMap.values()){
            if(chess.getChessId() == eChineseChessPiecesType.King.getValue()){
                return true;
            }
        }
        return false;
    }

    private int checkSelfMove(int x,int y,boolean isPowerOn){
        if(practiceData.getGameStatus() != 0){
            return GameErrorCode.E_CHINESECHESS_GAME_MOVE;
        }
        Map<Integer, ChineseChessData> chessMap = practiceData.getChessMap();
        ChineseChessData selfChess = chessMap.get(0);
        //判断是不是直线运动
        if(selfChess.getPosition().getX() != x && selfChess.getPosition().getY() != y){
            return GameErrorCode.E_CHINESECHESS_GAME_MOVE;
        }
        //是不是越界了
        if(x < 0 || y < 0 || x >= ChineseChessMgr.getX() || y >= ChineseChessMgr.getY()){
            return GameErrorCode.E_CHINESECHESS_GAME_MOVE;
        }
        //检验移动是不是出现前面有棋子挡住
        if(!isPowerOn){
            for(ChineseChessData chess :chessMap.values()){
                if(chess.getChessNo() == 0){
                    if(chess.getPosition().getX() == x && chess.getPosition().getY() == y){
                        return GameErrorCode.E_CHINESECHESS_GAME_MOVE;
                    }
                    continue;
                }
                if(selfChess.getPosition().getX() == x){//y轴运动
                    if(y > selfChess.getPosition().getY() && chess.getPosition().getX() == x
                            && chess.getPosition().getY() < y && chess.getPosition().getY() > selfChess.getPosition().getY()){
                        return GameErrorCode.E_CHINESECHESS_GAME_MOVE;
                    }
                    if(y < selfChess.getPosition().getY() && chess.getPosition().getX() == x
                            && chess.getPosition().getY() > y && chess.getPosition().getY() < selfChess.getPosition().getY()){
                        return GameErrorCode.E_CHINESECHESS_GAME_MOVE;
                    }
                }else if(selfChess.getPosition().getY() == y){//x轴运动
                    if(x > selfChess.getPosition().getX() && chess.getPosition().getY() == y
                            && chess.getPosition().getX() < x && chess.getPosition().getX() > selfChess.getPosition().getX()){
                        return GameErrorCode.E_CHINESECHESS_GAME_MOVE;
                    }
                    if(x < selfChess.getPosition().getX() && chess.getPosition().getY() == y
                            && chess.getPosition().getX() > x && chess.getPosition().getX() < selfChess.getPosition().getX()){
                        return GameErrorCode.E_CHINESECHESS_GAME_MOVE;
                    }
                }else{
                    return GameErrorCode.E_CHINESECHESS_GAME_MOVE;
                }
            }
        }
        return 0;
    }

    private void makeNextNewVersion(){
        Map<Integer,ChineseChessData> nextChessMap = ChineseChessMgr.getNextChessMap(practiceData.getStep(),practiceData.getChessCount());
        if(nextChessMap == null){
            practiceData.setNextChessTurn(0);
            practiceData.setNextChessMap(new ConcurrentHashMap<>());
        }else{
            practiceData.setNextChessTurn(ChineseChessMgr.getNextChessTurn(practiceData.getStep()));
            practiceData.setChessCount(practiceData.getChessCount() + nextChessMap.size());
            practiceData.setNextChessMap(nextChessMap);
        }
    }

    public int gameOperate(int activityId,int gameNo,int type,int skinId){
        if(type != 3 && (activityId != practiceData.getActivityId() || (!ChineseChessMgr.activityInRewardPeriod() && practiceData.getGameNo() != gameNo))){
            return GameErrorCode.E_CHINESECHESS_GAME_ERR;
        }
        ChineseChessProto.ChineseChessGameOperateRespMsg.Builder respMsg = ChineseChessProto.ChineseChessGameOperateRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setGameNo(practiceData.getGameNo());
        if(type == 0){//结束游戏
            if(!ChineseChessMgr.activityInRewardPeriod() && eChineseChessGameStatusType.Revive.getValue() != practiceData.getGameStatus() && eChineseChessGameStatusType.Pass.getValue() != practiceData.getGameStatus()){
                return GameErrorCode.E_CHINESECHESS_GAME_ERR;
            }
            ChineseChessProto.CrossChineseChessGetRankChangeReqMsg.Builder msg = ChineseChessProto.CrossChineseChessGetRankChangeReqMsg.newBuilder();
            msg.setActivityId(activityId);
            msg.setConditionType(eGamePlayerEventType.ChineseChessTopScoreRank.getValue());
            msg.setGameNo(practiceData.getGameNo());
            msg.setLastScore(0);
            msg.setNowScore(practiceData.getScore().longValue());
            msg.setIsPractice(true);
            player.sendPacket(CrossProtocol.C2_CHINESECHESS_ACTIVITY_GAME_OVER,msg);
            return 0;
        }
        else if(type == 1){//元宝重生
            if(ChineseChessMgr.isIngotRevive(practiceData.getIngotReviveTimes())){
                if(eChineseChessGameStatusType.Revive.getValue() != practiceData.getGameStatus()){
                    return GameErrorCode.E_CHINESECHESS_GAME_ERR;
                }
                practiceData.setGameStatus(eChineseChessGameStatusType.Running.getValue());
                practiceData.setIngotReviveTimes(practiceData.getIngotReviveTimes() + 1);
                ChineseChessData playerChess = practiceData.getChessMap().get(0);
                ChineseChessData removeChess = null;
                for(ChineseChessData chessData : practiceData.getChessMap().values()){
                    if(chessData.getChessNo() == 0)continue;
                    if(chessData.getPosition().getX() == playerChess.getPosition().getX() && chessData.getPosition().getY() == playerChess.getPosition().getY()){
                        removeChess = chessData;
                        continue;
                    }
                    respMsg.addChess(getChessModule().parseChineseChessDataPb(chessData));
                }
                List<ChineseChessData> logEatChessList = new ArrayList<>();
                if(removeChess != null){
                    logEatChessList.add(removeChess);
                    ChineseChessConfig removeChessConfig = ChineseChessMgr.getChessConfig(removeChess.getChessId());
                    practiceData.setScore(BigInteger.valueOf(practiceData.getScore().longValue() + getChessModule().getSkinAddScore(removeChessConfig)));
                    if(removeChess.getChessId() == eChineseChessPiecesType.King.getValue()){
                        practiceData.setPowerTimes(ChineseChessMgr.getEatKingPowerTime());
                    }
                    practiceData.getChessMap().remove(removeChess.getChessNo());
                    respMsg.setScore(practiceData.getScore().longValue());
                }
            }else{
                return GameErrorCode.E_CHINESECHESS_GAME_ERR;
            }
        }
        player.sendPacket(Protocol.U_CHINESECHESS_GAME_OPERATE, respMsg);
        return 0;
    }

    public void gameOver(int gameNo,int lastRank,int nowRank,int topRank){
        if(!ChineseChessMgr.activityInRewardPeriod() && eChineseChessGameStatusType.Revive.getValue() != practiceData.getGameStatus() && eChineseChessGameStatusType.Pass.getValue() != practiceData.getGameStatus()){
            return;
        }
        boolean isFirstPass = false;
        if(practiceData.getPassTime() == 1 && practiceData.getGameStatus() == eChineseChessGameStatusType.Pass.getValue()){
            isFirstPass = true;
        }
        practiceData.setGameStatus(eChineseChessGameStatusType.Over.getValue());
        ChineseChessProto.ChineseChessGameOperateRespMsg.Builder respMsg = ChineseChessProto.ChineseChessGameOperateRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setGameNo(gameNo);
        respMsg.setScore(practiceData.getScore().longValue());
        respMsg.setLastRank(lastRank);
        respMsg.setNowRank(nowRank);
        respMsg.setTopRank(topRank);
        respMsg.setIsFirstPass(isFirstPass);
        player.sendPacket(Protocol.U_CHINESECHESS_GAME_OPERATE_PRACTICE, respMsg);
    }


    @Override
    public boolean afterLoadData() {
        return false;
    }

    @Override
    public boolean loadData() {
        if(SystemOpenMgr.systemOpen(player, eSystemId.ChineseChess.getValue())){
            ActivityInfo activityInfo = ChineseChessMgr.getActivityInfo();
            if(activityInfo != null){
                practiceData = ChineseChessBusiness.getPracticeUserData(getUserId(),activityInfo.getActivityId());
                if(practiceData == null){
                    initPracticeUserData(activityInfo.getActivityId());
                }
            }
        }
        return true;
    }

    @Override
    public boolean saveData() {
        if(practiceData != null){
            if(practiceData.isInsertOption()){
                ChineseChessBusiness.addPracticeUserData(practiceData);
            }else if(practiceData.isUpdateOption()){
                ChineseChessBusiness.updatePracticeUserData(practiceData);
            }
        }
        return true;
    }

    @Override
    public void afterLogin() {
        if(SystemOpenMgr.systemOpen(player, eSystemId.ChineseChess.getValue())){
            ActivityInfo activityInfo = ChineseChessMgr.getActivityInfo();
            if(activityInfo != null){
                initPracticeUserData(activityInfo.getActivityId());
            }
        }
    }
}
